/**
 * 
 */
package Media.ui;

import java.util.Random;


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import agg.UIUtils;

import Library.Device;
import Media.ui.widgets.DeviceView.AddDevice;
import Media.ui.widgets.DeviceView.DeviceView;
import Media.ui.widgets.DeviceView.EditDevice;





/**
 * @author Hardeep
 *
 */
public class ManageDevices extends UIExtendable {
	
//	private Shell shell = null;
	private Canvas canvas = null;
	private ScrolledComposite scView = null;
	private ScrolledComposite scSync = null;
	private Composite child = null;
	private DeviceView devView = null;
	private DeviceView syncView = null;
	private Label topText = null;
	private Group devicesGroup = null;
	private Group syncsGroup = null;
	private Label syncLabel = null;
	private Button closeButton = null;
	private Button syncAddButton = null;
	private Button syncEditButton = null;
	private Button syncDelButton = null;
	private Button devAddButton = null;
	private Button devEditButton = null;
	private Button devDelButton = null;
	
	public ManageDevices() {
		createShell();
	}

	public void createShell() {
		final int window_width = 450;
		final int window_height = 650;
		// Main Shell Window
		shell = new Shell();//SWT.CLOSE | SWT.TITLE | SWT.MIN);
		shell.setMinimumSize(window_width, window_height);
		shell.setText("Manage Devices");
		
		shell.addControlListener(new ControlListener() {
			public void controlMoved(ControlEvent arg0) {}
			public void controlResized(ControlEvent arg0) {
				// TODO re-enable meh
//				shell.setSize(window_width, window_height);
			}
		});
		
		// The Grid Layout
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
//		gridLayout.makeColumnsEqualWidth = true;
		shell.setLayout(gridLayout);
		
		// Create and show the header
		createHeader();
		
		// Create the devices group of widgets
		createDevicesGroup();
		
		// Create the device sync'ing group of widgets
		createSyncsGroup();
		
		// Button to close the window
		GridData dataBtnClose = new GridData();
		dataBtnClose = UIUtils.createGridData(false, false, GridData.FILL, 
				GridData.CENTER, 3, -1, -1, -1, -1, -1, -1, -1);
		
		closeButton = new Button(shell, SWT.NONE);
		closeButton.setText("Close");
		closeButton.setLayoutData(dataBtnClose);
		closeButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {}
			public void widgetSelected(SelectionEvent arg0) {
				shell.dispose();
			}
		});
	}
	
	private void createHeader() {
		// Icon data for top header
	    ImageData iconData = new ImageData(
	    		getClass().getResourceAsStream(UIUtils.res_path + "header_manage_devices.png"));
		final Image iconImage = new Image(Display.getCurrent(),iconData);
		
		// Canvas to display icon
		canvas = new Canvas(shell, SWT.NONE);
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.drawImage(iconImage,0,0);
			}
		});
		
		// Header text Grid Data
		GridData gridData = new GridData();
		gridData = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.BEGINNING, 2, -1, -1, -1, -1, -1, -1, -1);
		
		// Header text
		topText = new Label(shell, SWT.WRAP);
		topText.setText("Add as many devices as you want, and then set up" +
				" sync relationships between them. The libraries for each" +
				" device are independant and can be modified in any way that" +
				" you like.");
		topText.setLayoutData(gridData);
	}

	private void createDevicesGroup() {
		// The Grid Layout
		GridLayout layoutDevGroup = new GridLayout();
		layoutDevGroup.numColumns = 3;
		layoutDevGroup.makeColumnsEqualWidth = true;
		
		// Group Contained Layout Data
		GridData dataDevGroup = new GridData();
		dataDevGroup = UIUtils.createGridData(true, true, GridData.FILL, 
				GridData.FILL, 3, -1, -1, -1, -1, -1, -1, -1);
		
		// Grid Data for each button
		GridData devDelData = new GridData();
		devDelData = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData devEdtData = new GridData();
		devEdtData = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData devAddData = new GridData();
		devAddData = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		// Group Container Object
		devicesGroup = new Group(shell, SWT.NONE);
		devicesGroup.setText("Devices");
		devicesGroup.setLayout(layoutDevGroup);
		devicesGroup.setLayoutData(dataDevGroup);
		
		// Canvas
		createDevicesView();
		
		// Add Devices button
		devAddButton = new Button(devicesGroup, SWT.NONE);
		devAddButton.setText("Add");
		devAddButton.setLayoutData(devAddData);
		devAddButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
				widgetSelected(arg0);
			}
			public void widgetSelected(SelectionEvent arg0) {
				AddDevice ad = new AddDevice(devView);
				ad.setParentShell(shell);
				ad.open();
			}			
		});
		
		// Edit Devices button
		devEditButton = new Button(devicesGroup, SWT.NONE);
		devEditButton.setText("Edit");
		devEditButton.setLayoutData(devEdtData);
		devEditButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
				widgetSelected(arg0);
			}
			public void widgetSelected(SelectionEvent arg0) {
				Device d = devView.getSelectedDevice();
				UIExtendable ed = new EditDevice(devView, d);
				ed.setParentShell(shell);
				ed.open();
			}			
		});
		
		// Delete Devices button
		devDelButton = new Button(devicesGroup, SWT.NONE);
		devDelButton.setText("Delete");
		devDelButton.setLayoutData(devDelData);
	}

	private void createSyncsGroup() {
		// The Grid Layout
		GridLayout layoutSyncGroup = new GridLayout();
		layoutSyncGroup.numColumns = 3;
		layoutSyncGroup.makeColumnsEqualWidth = true;
		
		// Group Container Layout Data
		GridData dataSyncGroup = new GridData();
		dataSyncGroup = UIUtils.createGridData(true, true, GridData.FILL, 
				GridData.FILL, 3, -1, -1, -1, -1, -1, -1, -1);
		
		// Grid Data for each button
		GridData synDelData = new GridData();
		synDelData = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData synAddData = new GridData();
		synAddData = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData synEdtData = new GridData();
		synEdtData = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		// Helper text Data
		GridData gridData3 = new GridData();
		gridData3 = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, 3, -1, -1, -1, -1, -1, -1, -1); 
		
		// Group Container Object
		syncsGroup = new Group(shell, SWT.NONE);
		syncsGroup.setText("Sync Relationships");
		syncsGroup.setLayout(layoutSyncGroup);
		syncsGroup.setLayoutData(dataSyncGroup);
		
		// Helper Text
		syncLabel = new Label(syncsGroup, SWT.NONE);
		syncLabel.setText("Select one of the devices above to see which other" +
				" devices it syncs with.");
		syncLabel.setLayoutData(gridData3);
		
		// Canvas
		createSyncRelationsView();
		
		// Edit Sync Relationship button
		syncEditButton = new Button(syncsGroup, SWT.NONE);
		syncEditButton.setText("Add");
		syncEditButton.setLayoutData(synEdtData);
		
		// Add Sync Relationship button
		syncAddButton = new Button(syncsGroup, SWT.NONE);
		syncAddButton.setText("Edit");
		syncAddButton.setLayoutData(synAddData);

		// Delete Sync Relationship button
		syncDelButton = new Button(syncsGroup, SWT.NONE);
		syncDelButton.setText("Delete");
		syncDelButton.setLayoutData(synDelData);
	}

	private void createDevicesView() {
		GridData gData = new GridData();
		gData = UIUtils.createGridData(true, true, GridData.FILL, GridData.FILL, 
				3, -1, -1, -1, -1, -1, -1, -1);
		
		devView = new DeviceView(devicesGroup, SWT.BORDER);
		devView.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		devView.setLayoutData(gData);
	}

	private void createSyncRelationsView() {
		GridData gData = new GridData();
		gData = UIUtils.createGridData(true, true, GridData.FILL, GridData.FILL, 
				3, -1, -1, -1, -1, -1, -1, -1);
		
		syncView = new DeviceView(syncsGroup, SWT.BORDER);
		syncView.setAsSyncView(devView);
		syncView.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		syncView.setLayoutData(gData);
	}
}
